package com.sx.manage.service.operation;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.common.util.json.JackJsonUtil;
import com.sx.manage.biz.consumer.info.IMNetworkPersonConsumerBiz;
import com.sx.manage.biz.consumer.relation.IMConsumerDataRelationBiz;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.consumer.info.IMConsumerOperationInfoService;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.manage.mybatis.form.consumer.MPersonalConsumerNetworkForm;
import com.sx.model.manage.mybatis.form.sys_operation.MSysAccountInfoForm;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerOperationInfoVO;
import com.sx.model.manage.mybatis.vo.consumer.MPersonalConsumerNetworkVO;
import com.sx.model.manage.mybatis.vo.operation.MSysAccountInfoVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
/**
 * 账户信息查询
 * @author zwm
 *
 */
@Service("sysAccountInfoService")
public class SysAccountInfoService {
	private Logger _log = LoggerFactory.getLogger(this.getClass());
	//用户信息新接口
	@Resource(name="mNetworkPersonConsumerBizImpl")
	public IMNetworkPersonConsumerBiz mNetworkPersonConsumerBizImpl;
	//@Resource(name="accountService")
	//private IAccountServiceInterface accountService;
	
	@Resource(name="mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;
	
	//二期 用户各种状态查询
	@Resource(name="consumerOperationInfoServiceImpl")
	private IMConsumerOperationInfoService consumerOperationInfoServiceImpl;
	//用户关系数据
	@Resource(name="mConsumerDataRelationBizImpl")
	private IMConsumerDataRelationBiz mConsumerDataRelationBizImpl;
	
	@Resource(name="webServiceUtils")
	private WebServiceUtils webServiceUtils;
	/**
	 * 账户信息查询
	 */
	public ResultListVO<MSysAccountInfoVO> search_sys_account(MSysAccountInfoForm form) {
		//根据查询条件查询-用户信息-查询银行信息-查询账号状态
		_log.debug("添加系统操作日志：SysAccountInfoService===search_sys_account==formJson="+JackJsonUtil.objToJson( form ));
		if (null == form) {
			return new ResultListVO<>("sj.m.common.query.error");
		}
		MPersonalConsumerNetworkForm pcForm = new MPersonalConsumerNetworkForm();
		if ("" != form.getConsumer_name()) {
			pcForm.setConsumer_name(form.getConsumer_name());
		}else{
			pcForm.setConsumer_name(null);
		}
		if ("" != form.getMobile_tel()) {
			pcForm.setMobile_tel(form.getMobile_tel());
		}else{
			pcForm.setMobile_tel(null);
		}
		pcForm.setLaw_no(form.getLaw_no());
		pcForm.setStart(form.getStart());
		pcForm.setLength( form.getLength());
		ResultListVO<MPersonalConsumerNetworkVO> personalResult =  mNetworkPersonConsumerBizImpl.getConsumerPageNetworkVO( pcForm );
		List<MPersonalConsumerNetworkVO> list = personalResult.getData();
		if (list.isEmpty()) {
			return new ResultListVO<>(true,"sj.m.common.query_.success","删除成功");
		}
		List<MSysAccountInfoVO> sysAccountList = new ArrayList<>();
		for (MPersonalConsumerNetworkVO vo:list) {
			MSysAccountInfoVO mvo = new MSysAccountInfoVO();
			if (null != vo) {
				MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumerBankMap_for_manage(vo.getId(), null);
				
				MConsumerOperationInfoVO operationVO = consumerOperationInfoServiceImpl.queryByConsumerId( vo.getId() ).getObjVo();
				if( null != operationVO ){
					mvo.setKey_validate( operationVO.getKey_validate());
				}
				mvo.setAccount_no( webServiceUtils.queryAccountNo( bankMapVO.getConsumer_id()));
				mvo.setBank_account_no( webServiceUtils.queryBranchAccountNo(bankMapVO.getConsumer_id()) );
				mvo.setTradeMemCode(webServiceUtils.queryTradeMemberCode(bankMapVO.getConsumer_id()));
				mvo.setConsumer_name(vo.getConsumer_name());
				mvo.setMobile_tel(vo.getMobile_tel());
				
				mvo.setLaw_no(vo.getLaw_no());
				mvo.setAccount_no_status( bankMapVO.getAccount_no_status());//vo.getBank().getAccount_no_status()
//				CAccountInfoEntity accountInfo = accountService.getAccountInfo( bankMapVO.getAccount_no() );//vo.getBank().getAccount_no()
//				if( null != accountInfo ){
//					mvo.setAccountStatus(accountInfo.getStatus());
//				}
			}
			sysAccountList.add(mvo);
		}
		ResultListVO<MSysAccountInfoVO>  listVO = new ResultListVO<>();
		listVO.setSuccess(true);
		listVO.setMsg("查询成功");
		listVO.setData(sysAccountList);
		listVO.setRecordsTotal( personalResult.getRecordsTotal());
		listVO.setRecordsFiltered( personalResult.getRecordsFiltered());
		return listVO;
	}
	
	

}
